package cn.com.usermanage.impl;
 
import cn.com.common.ConnectionPool;
import cn.com.sqlinterface.SQLInterface;
import cn.com.user.Poke_User;
import cn.com.usermanage.UserManager;
import com.server.Board_Table;
import com.server.PokeServer;
//import java.io.PrintStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 

/**
 * 用户管理类
 * @author jj043
 *
 */
 public class UserManagerImpl implements UserManager
 {
        private Logger logger = LoggerFactory.getLogger(UserManagerImpl.class);
        public static ConnectionPool conpool;
        boolean on_off = true;
 
     /**
      * "update poke_user set user_all_money=user_all_money+?  where user_id = ?";
      * 更新用户的钱数
      * @param user
      * @return
      */   
     public int backAllMoney(Poke_User user) 
     { 
    	     PreparedStatement ps = null;
 
             String sql = "";
		     Connection con = null;
		     int i = 0;
		     con = conpool.getConnection();
		     if (con == null)
		     {
		       this.logger.error("  backAllMoney  return null conneciont to db");
		       conpool.returnConnectionPool(con);
		       con = null;
		       return 0;
             }
		     
		     try 
		     {
		       sql = SQLInterface.BACK_ALL_MONEY;// "update poke_user set user_all_money=user_all_money+?  where user_id = ?";
		       ps = con.prepareStatement(sql);
		       ps.setInt(1, user.getIn_money());
		       ps.setInt(2, user.getUser_id());
		       i = ps.executeUpdate();
		 
		       user.setUser_rest(0);
		     } catch (Exception e) 
		     {
		       e.printStackTrace();
		       this.logger.error(" backAllMoney 执行时候出错：{}", e.getMessage());
		       i = 0;
		     }
		     closeDBRS(ps);
		     conpool.returnConnectionPool(con);
		     con = null;
		     return i; 
   			}
 

     /**
      * 查询用户的钱
      * @param user
      * @return
      */
   public int selectAllMoney(Poke_User user)
   {
     PreparedStatement ps = null;
     ResultSet rs = null;
     String sql = "";
     int i = 0;
     Connection con = null;
     con = conpool.getConnection();
     if (con == null) 
     {
       this.logger.error("  selectAllMoney  return null conneciont to db");
       conpool.returnConnectionPool(con);
       con = null;
       return 0;
     }
     try 
     {
       sql = SQLInterface.SELECT_ALL_MEONEY;// "select user_all_money from poke_user where user_id = ?";
       ps = con.prepareStatement(sql);
 
       ps.setInt(1, user.getUser_id());
       rs = ps.executeQuery();
 
       while (rs.next())
         i = rs.getInt(1);
     }
     catch (Exception e) 
     {
       e.printStackTrace();
       this.logger.error(" selectAllMoney 执行时候出错：{}", e.getMessage());
       i = 0;
     }
     closeDBRS(rs, ps);
     conpool.returnConnectionPool(con);
     con = null;
     return i;
   }
 
   /**
    * 查询用户的所有信息，用来初始化用户
    * @param user
    * @return
    */
   public Poke_User actInitUser(Poke_User user)
   {
     PreparedStatement ps = null;
     ResultSet rs = null;
     String sql = "";
    // int i = 0;
     Connection con = null;
     con = conpool.getConnection();
     if (con == null) 
     {
       this.logger.error("  actInitUser  return null conneciont to db");
       conpool.returnConnectionPool(con);
       con = null;
       return null;
     }
     try 
     {
       sql =SQLInterface.INIT_USER;// "select USER_ID,USER_NAME,USER_PASS,USER_PICTURE,USER_ADD,USER_AGE,USER_ALL_MONEY,IS_VIP,user_legend,community,exp_value,charm_value,card_coin from poke_user where USER_ID=?";
       ps = con.prepareStatement(sql);
       ps.setInt(1, user.getUser_id());
 
       rs = ps.executeQuery();
               
       while (rs.next()) 
       {
         user.setUser_id(rs.getInt(1));
         user.setUser_name(rs.getString(2));
         user.setUser_pass(rs.getString(3));
         user.setUser_pcture(rs.getString(4));
         user.setUser_add(rs.getString(5));
         user.setUser_age(rs.getInt(6));
         user.setTotal_momey(rs.getInt(7));
         user.setIs_vip(rs.getInt(8));
         user.setUser_level(getUserLevel(user.getTotal_momey()));
         user.setSex(rs.getString(9));
         user.setUserTitle(getUserTitle(user.getUser_level(), user.getSex()));
         user.setUser_commucity(rs.getString(10));
         user.setExp_value(rs.getInt(11));
         user.setChar_value(rs.getInt(12));
         user.setCard_coin(rs.getInt(13));                   
         user.setHome_add(rs.getString(14));
       }
     }
     catch (Exception e) 
     {
       closeDBRS(rs, ps);
       conpool.returnConnectionPool(con);
       this.logger.error("method:actInitUser throw error:{}", e.getMessage());
 
       if (e.getMessage().contains("OutOfMemoryError"))
       {
         System.gc();
       }
       return null;
     }
     closeDBRS(rs, ps);
     conpool.returnConnectionPool(con);
     return user;
   }
 
/**
 * 初始化用户之前减掉买入的钱
 * 这一步多余要优化
 */
   public Poke_User initUser(Poke_User user)
   {
     PreparedStatement ps = null;
     ResultSet rs = null;
     Connection con = null;
     try
     {
       String sql = null;
 
       if (user.getUser_rest() > 0)
       {
         try
         {
//           int i = 0;
           backAllMoney(user);//i = backAllMoney(user);
         } catch (Exception e) 
         {
           e.printStackTrace();
           this.logger.error("  initUser 调用  backAllMoney 出错：{} ", e.getMessage());
         }
 
       }
 
       try
       {
         user.setTotal_momey(selectAllMoney(user));
       } catch (Exception e) 
       {
         e.printStackTrace();
         this.logger.error("  initUser 调用  backAllMoney 出错：{} ", e.getMessage());
         return null;
       }
 
       if (user.getIn_money() <= user.getTotal_momey()) 
       {
         sql =SQLInterface.UPDATE_USERMONEY;// "update poke_user set user_all_money=? where user_id=?";
         con = conpool.getConnection();
 
         ps = con.prepareStatement(sql);
         ps.setInt(1, user.getTotal_momey() - user.getIn_money());
         ps.setInt(2, user.getUser_id());
         ps.executeUpdate();//i = ps.executeUpdate();
       }
       else
       {
         closeDBRS(rs, ps);
         conpool.returnConnectionPool(con);
         con = null;
         return null;
       }
 
       user = actInitUser(user);
     }
     catch (SQLException e) {
       e.printStackTrace();
       closeDBRS(rs, ps);
       conpool.returnConnectionPool(con);
       this.logger.error("method:initUser throw error:{}", e.getMessage());
 
       if (e.getMessage().contains("OutOfMemoryError"))
       {
         System.gc();
       }
       return null;
     }
     closeDBRS(rs, ps);
     conpool.returnConnectionPool(con);
     con = null;
 
     return user;
   }
  
   /**
    * 查询是否已登陆
    * @param user
    * @return
    */
   public int initUserinfobase(Poke_User user) 
   {
     PreparedStatement ps = null;
     ResultSet rs = null;
     String sql = null;
     Connection con = null;
     try {
       con = conpool.getConnection();
       if (con == null) 
       {
         closeDBRS(rs, ps);
         this.logger.error("initUserInfo  return null conneciont to db");
         conpool.returnConnectionPool(con);
         con = null;
         return 0;
       }
       sql =SQLInterface.SLECT_COUNT;// "select count(*) from table_user_ref where user_id=?";
       ps = con.prepareStatement(sql);
       ps.setInt(1, user.getUser_id());
       rs = ps.executeQuery();
       while (rs.next())
         if (rs.getInt(1) > 0) 
         {
           closeDBRS(rs, ps);
           conpool.returnConnectionPool(con);
           con = null;
           return 0;
         }
     } catch (Exception e) {
       e.printStackTrace();
       this.logger.error("   initUserinfobase  {} ", e.getMessage());
       closeDBRS(rs, ps);
       conpool.returnConnectionPool(con);
       return 0;
     }
 
     closeDBRS(rs, ps);
     conpool.returnConnectionPool(con);
     return 1;
   }
 
   /**
    * 查询用户信息
    */
   public Poke_User initUserInfo(Poke_User user)
   {
     PreparedStatement ps = null;
     ResultSet rs = null;
     Poke_User user1 = null;
     String sql = null;
     Connection con = null;
     try
     {
       sql = SQLInterface.INIT_USER;//"select USER_ID,USER_NAME,USER_PASS,USER_PICTURE,USER_ADD,USER_AGE,USER_ALL_MONEY,IS_VIP,user_legend,community,exp_value,charm_value,card_coin from poke_user where USER_ID=?";
       con = conpool.getConnection();
       if (con == null) 
       {
         closeDBRS(rs, ps);
         this.logger.error("initUserInfo  return null conneciont to db");
         conpool.returnConnectionPool(con);
         con = null;
         return null;
       }
       ps = con.prepareStatement(sql);
       ps.setInt(1, user.getUser_id());
 
       rs = ps.executeQuery();
       user1 = new Poke_User();
       while (rs.next())
       {
         user1.setUser_id(rs.getInt(1));
         user1.setUser_name(rs.getString(2));
         user1.setUser_pass(rs.getString(3));
         user1.setUser_pcture(rs.getString(4));
         user1.setUser_add(rs.getString(5));
         user1.setUser_age(rs.getInt(6));
         user1.setTotal_momey(rs.getInt(7));
         user1.setIs_vip(rs.getInt(8));
         user1.setUser_level(getUserLevel(user1.getTotal_momey()));
 
         user1.setSex(rs.getString(9));
         user1.setUserTitle(getUserTitle(user1.getUser_level(), user1.getSex()));
         user1.setUser_commucity(rs.getString(10));
         user1.setExp_value(rs.getInt(11));
         user1.setChar_value(rs.getInt(12));
         user1.setCard_coin(rs.getInt(13));
         System.out.println(user1.getUser_level() + "\t" + user1.getSex() + "\t" + user1.getUserTitle() + "\t" + user1.getTotal_momey() + "\t" + user1.getUser_commucity());
       }
     }
     catch (SQLException e) {
       e.printStackTrace();
       this.logger.error("method:initUserInfo throw error:{}", e.getMessage());
       if (e.getMessage().contains("OutOfMemoryError"))
       {
         System.gc();
       }
     }
     closeDBRS(rs, ps);
     conpool.returnConnectionPool(con);
     con = null;
     return user1;
   }
 
   public static void main(String[] args)
   {
     Poke_User user = new Poke_User();
     user.setUser_id(1);
     user.setIn_money(400);
    // new UserManagerImpl().initUser(user);
    // System.out.println(user.getUser_name() + "\t" + user.getIs_vip() + "\t");
   }
 
   /**
    * 查询桌子信息
    */
   public Board_Table initTable(int tableid)
   {
     PreparedStatement ps = null;
     ResultSet rs = null;
 
     String sql = null;
     Board_Table table = null;
     Connection con = null;
     try
     {
       con = conpool.getConnection();
       if (con == null) 
       {
         closeDBRS(rs, ps);
         this.logger.error("initTable  return null conneciont to db");
         conpool.returnConnectionPool(con);
         con = null;
         return null;
       }
 
       sql = SQLInterface.INIT_TABLE;//"select table_id,table_name,max_handle,min_handle,big_nate,little_nate,rate,max_link,table_type,community,consume_money from poke_tables where table_id =?";
       ps = con.prepareStatement(sql);
       ps.setInt(1, tableid);
 
       rs = ps.executeQuery();
       table = new Board_Table(new Object());
       while (rs.next())
       {
         table.setId(rs.getInt(1));
         table.setTable_name(rs.getString(2));
         table.setBuymax(rs.getInt(3));
         table.setBuymin(rs.getInt(4));
         table.setBig_nate(rs.getInt(5));
         table.setLittle_nate(rs.getInt(6));
         table.setCall_money((int)Math.ceil(rs.getFloat(7) * table.getLittle_nate()));
 
         table.setMaxconnection(rs.getInt(8));
         table.setTable_type(rs.getString(9));
         table.setCommunity(rs.getString(10));
         table.setConsume_money(rs.getInt(11));
       }
     } catch (SQLException e) 
     {
       e.printStackTrace();
 
       this.logger.error("method:initTable throw error:{}", e.getMessage());
       if (e.getMessage().contains("OutOfMemoryError"))
       {
         System.gc();
       }
       closeDBRS(rs, ps);
       conpool.returnConnectionPool(con);
       con = null;
       return null;
     }
     closeDBRS(rs, ps);
     conpool.returnConnectionPool(con);
     con = null;
 
     return table;
   }
 
  	/**
	 * 得到用户的等级
	 */  
 public  int getUserLevel( int flag )
 {
	
	return flag < 3000 ? 1 : 
			flag < 10000 ? 2 :
			flag < 50000 ? 3 :
		    flag < 100000 ? 4 :
			flag < 250000 ? 5 :
			flag < 500000 ? 6 :
			flag < 750000 ? 7 :
			flag < 1000000 ? 8 :
			flag < 2500000 ? 9 :
			flag < 5000000 ? 10 :
			flag < 7500000 ? 11 :
			flag < 10000000 ? 12 :
			flag < 25000000 ? 13 :
			flag < 50000000 ? 14 :
			flag < 75000000 ? 15 :
			flag < 100000000 ? 16 :
			flag < 300000000 ? 17 :	
			flag < 600000000 ? 18:
			flag < 1000000000 ? 19:
			flag < 150000000 ? 20 :21
			;			
}
 
 
 /**
  * 更新用户的钱
  */
   public void backUsermoney(Poke_User user)
   {
     PreparedStatement ps = null;
     ResultSet rs = null;
     int i = 0;
    // String sql = null;
     Connection con = null;
     try
     {
       int money = 0;
       money = user.getIn_money();
       if (user.getUser_rest() > 0)
       {
         try
         {
           i = backAllMoney(user);
         } catch (Exception e) {
           e.printStackTrace();
           this.logger.error("  initUser 调用  backAllMoney 出错：{} ", e.getMessage());
         }
 
       }
 
       if (this.on_off)
         System.out.println("执行归还用户钱：  " + i);
       money = user.getTotal_momey() + money;
       Object[] arr = { user.getUser_id() + "/" + user.getUser_name(), Integer.valueOf(money) };
       this.logger.info("{}用户离开时拥有的钱：{}", arr);
     } catch (Exception e) 
     {
       e.printStackTrace();
       conpool.returnConnectionPool(con);
       this.logger.error("method:backUsermoney throw error:{}", e.getMessage());
       if (e.getMessage().contains("OutOfMemoryError"))
       {
         System.gc();
       }
     }
     closeDBRS(rs, ps);
     conpool.returnConnectionPool(con);
     con = null;
   }
 
   /**
    * 登陆成功
    */   
   public int UserLoginSuccess(int tableid, int user_id,String user_name,String user_picture,int user_money,String user_sex,String user_home,int user_vip,String ip,int port) {
     PreparedStatement ps = null;
     int i = 0;
     String sql = null;
     Connection con = null;
     try {
       con = conpool.getConnection();
       if (con == null) {
         closeDBRS(ps);
         conpool.returnConnectionPool(con);
         con = null;
         this.logger.error("UserLoginSuccess  return null conneciont to db");
         return 0;
       }
       sql =SQLInterface.INSER_REF_RECOD;// "insert into  table_user_ref(table_id,user_id,table_type)values(?,?,2)";
       ps = con.prepareStatement(sql);                
                 System.out.println("user_name:"+user_name);
                 System.out.println("user_picture:"+user_picture);
                 System.out.println("user_money:"+user_money);
                 System.out.println("user_sex:"+user_sex);
                 System.out.println("user_home:"+user_home);
                 System.out.println("user_vip:"+user_vip);
                 System.out.println("user_vip:"+ip);
                 System.out.println("user_vip:"+port);
                 ps.setInt(1, tableid);
                 ps.setInt(2, user_id);
                 ps.setString(3,user_name);
                 ps.setString(4,user_picture);
                 ps.setInt(5,user_money);
                 ps.setString(6,user_sex);
                 ps.setString(7,user_home);
                 ps.setInt(8,user_vip);
                 ps.setString(9,ip);
                 ps.setInt(10, port);
       i = ps.executeUpdate();
       if (this.on_off)
         System.out.println("用户登陆成功：  " + i);
     }
     catch (SQLException e) {
       e.printStackTrace();
 
       this.logger.error("method:UserLoginSuccess throw error:{}", e.getMessage());
       if (e.getMessage().contains("OutOfMemoryError"))
       {
         System.gc();
       }
     }
     closeDBRS(ps);
     conpool.returnConnectionPool(con);
     con = null;
     return i;
   }
 
   /**
    * 坐下成功
    * 写入坐下关连表
    */
   public int seatSuccess(int user_id) 
   {
     PreparedStatement ps = null;
     int i = 0;
     String sql = null;
     Connection con = null;
     try {
       con = conpool.getConnection();
       if (con == null) {
         closeDBRS(ps);
         conpool.returnConnectionPool(con);
         con = null;
         this.logger.error("seatSuccess  return null conneciont to db");
         return 0;
       }
 
       sql = SQLInterface.SEAT_SUCCESS;// "update table_user_ref set User_State = 1 where user_id = ?";
       ps = con.prepareStatement(sql);
       ps.setInt(1, user_id);
 
       i = ps.executeUpdate();
       if (this.on_off) 
       {
         System.out.println("增加新的关联记录：  " + i);
       }
 
     }
     catch (SQLException e)
     {
       e.printStackTrace();
       this.logger.error("method:seatSuccess throw error:{}", e.getMessage());
       if (e.getMessage().contains("OutOfMemoryError"))
       {
         System.gc();
       }
     }
     closeDBRS(ps);
     conpool.returnConnectionPool(con);
     con = null;
     return i;
   }
 
   /**
    * 用户离开
    */
   public int userOut(int user_id)
   {
     PreparedStatement ps = null;
     int i = 0;
     String sql = null;
     Connection con = null;
     try {
       con = conpool.getConnection();
       if (con == null) 
       {
         closeDBRS(ps);
         conpool.returnConnectionPool(con);
         con = null;
         this.logger.error("userOut  return null conneciont to db");
         return 0;
       }
       sql = SQLInterface.USER_OUT;// "DELETE from table_user_ref where user_id = ?";
       ps = con.prepareStatement(sql);
       ps.setInt(1, user_id);
 
       i = ps.executeUpdate();
       if (this.on_off) 
       {
         System.out.println("。。。。。删除关联信息。。。。。" + i);
       }
     }
     catch (SQLException e)
     {
       e.printStackTrace();
       closeDBRS(ps);
       this.logger.error("method:userOut throw error:{}", e.getMessage());
       if (e.getMessage().contains("OutOfMemoryError"))
       {
         System.gc();
       }
     }
     closeDBRS(ps);
     conpool.returnConnectionPool(con);
     con = null;
     return i;
   }
 
   /**
    * 用户离开
    */
   public int checkUserOut(int user_id)
   {
     PreparedStatement ps = null;
 
     int i = 0;
     String sql = null;
     //int flag = 0;
     Connection con = null;
     try {
       con = conpool.getConnection();
       if (con == null) 
       {
         closeDBRS(ps);
         conpool.returnConnectionPool(con);
         con = null;
         this.logger.error("checkUserOut  return null conneciont to db");
         return 0;
       }
 
       sql = SQLInterface.USER_OUT;//"DELETE from table_user_ref where user_id = ?";
       ps = con.prepareStatement(sql);
       ps.setInt(1, user_id);
 
       i = ps.executeUpdate();
       if (this.on_off)
         System.out.println("。。。。。删除关联信息。。。。。" + i);
     }
     catch (SQLException e) 
     {
       conpool.returnConnectionPool(con);
       e.printStackTrace();
       closeDBRS(ps);
       this.logger.error("method:checkUserOut throw error:{}", e.getMessage());
       if (e.getMessage().contains("OutOfMemoryError"))
       {
         System.gc();
       }
     }
 
     closeDBRS(ps);
     conpool.returnConnectionPool(con);
     con = null;
     return i;
   }
 
   /**
    * 用户站起
    */
   public int userStand(int user_id)
   {
     PreparedStatement ps = null;
     int i = 0;
     String sql = null;
     Connection con = null;
     try 
     {
       con = conpool.getConnection();
       if (con == null) 
       {
         closeDBRS(ps);
         conpool.returnConnectionPool(con);
         con = null;
         this.logger.error("userStand  return null conneciont to db");
         return 0;
       }
 
       sql = SQLInterface.USER_STAND;//"update table_user_ref set User_State = 0 where user_id = ?";
       ps = con.prepareStatement(sql);
       ps.setInt(1, user_id);
 
       i = ps.executeUpdate();
       System.out.println("   用户站立:" + i);
     } catch (SQLException e) 
     {
       e.printStackTrace();
       this.logger.error("method:userStand throw error:{}", e.getMessage());
       if (e.getMessage().contains("OutOfMemoryError"))
       {
         System.gc();
       }
     }
     closeDBRS(ps);
     conpool.returnConnectionPool(con);
     con = null;
     return i;
   }
 
   /**
    * 关闭句柄
    * @param rs
    * @param ps
    */
   public void closeDBRS(ResultSet rs, PreparedStatement ps)
   {
     try {
       if ((rs != null) && 
         (!rs.isClosed())) {
         if (!rs.isClosed()) {
           rs.close();
         }
         rs = null;
       }
       if ((ps == null) || 
         (ps.isClosed()))
         return;
       if (!ps.isClosed()) {
         ps.close();
       }
       ps = null;
     }
     catch (Exception e) 
     {
       e.printStackTrace();
 
       this.logger.error("method: closeDBRS throws error:{}", e.getMessage());
     }
   }
 
   
   /**
    * 关闭连接
    * @param ps
    */
   public void closeDBRS(PreparedStatement ps)
   {
     try
     {
       if ((ps == null) || 
         (ps.isClosed()))
         return;
       if (!ps.isClosed())
       {
         ps.close();
       }
       ps = null;
     }
     catch (Exception e) 
     {
       e.printStackTrace();
 
       this.logger.error("method: closeDBRS throws error:{}", e.getMessage());
     }
   }
 
 	







/*

private String getUserTitle(int level,String legend){

    int a = (int) (Math.random()*2);

    boolean bb = false;

    if(a>1)
    {

          bb = true;

    }else{

          bb = false;

    }

    String userTitle = "None";

    switch (level) {

    case 1:

          if(legend.equals("男")){

                if(bb)

                userTitle="Private★1K★";

                else

                userTitle="Freshman★1K★";

          }

          else{

                if(bb)

                userTitle="Private★1K★";

                else

                userTitle="Freshman★1K★";

          }

          break;

    case 2:

          if(legend.equals("男")){

                if(bb)

                userTitle="Private First Class★10K★";

                else

                userTitle="Sophomore★10K★";

          }

          else{

                if(bb)

                userTitle="Private First Class★10K★";

                else

                userTitle="Sophomore★10K★";

          }

          break;

    case 3:

          if(legend.equals("男")){

                if(bb)

                userTitle="Lance Corporal★50K★";

                else

                userTitle="Junior★50K★";

          }

          else{

                if(bb)

                userTitle="Lance Corporal★50K★";

                else

                userTitle="Junior★50K★";

          }

          break;

    case 4:

          if(legend.equals("男")){

                if(bb)

                userTitle="Professional Corporal★100K★";

                else

                userTitle="Seniors★100K★";

          }

          else{

                if(bb)

                userTitle="Professional Corporal★100K★";

                else

                userTitle="Seniors★100K★";

          }

          break;

    case 5:

          if(legend.equals("男")){

                if(bb)

                userTitle="Corporal★250K★";

                else

                userTitle="Bachelor★250K★";

          }

          else{

                if(bb)

                userTitle="Corporal★250K★";

                else

                userTitle="Bachelor★250K★";

          }

          break;

    case 6:

          if(legend.equals("男")){

                if(bb)

                userTitle="Sergeant★500K★";

                else

                userTitle="Master★500K★";

          }

          else{

                if(bb)

                userTitle="Sergeant★500K★";

                else

                userTitle="Master★500K★";

          }

          break;

    case 7:

          if(legend.equals("男")){

                if(bb)

                userTitle="Staff Sergeant★750K★";

                else

                userTitle="Doctor★750K★";

          }

          else{

                if(bb)

                userTitle="Staff Sergeant★750K★";

                else

                userTitle="Doctor★750K★";

          }

          break;

    case 8:

          if(legend.equals("男")){

                if(bb)

                userTitle="Second Lieutenant★1M★";

                else

                userTitle="Architect★1M★";

          }

          else{

                if(bb)

                userTitle="Second Lieutenant★1M★";

                else

                userTitle="Architect★1M★";

          }

          break;

    case 9:

          if(legend.equals("男")){

                if(bb)

                userTitle="First Lieutenant★2.5M★";

                else

                userTitle="Principal★2.5M★";

          }

          else{

                if(bb)

                userTitle="First Lieutenant★2.5M★";

                else

                userTitle="Principal★2.5M★";

          }

          break;

    case 10:

          if(legend.equals("男")){

                if(bb)

                userTitle="Captain★5M★";

                else

                userTitle="Manager★5M★";

          }

          else{

                if(bb)

                userTitle="Captain★5M★";

                else

                userTitle="Manager★5M★";

          }

          break;

    case 11:

          if(legend.equals("男")){

                if(bb)

                userTitle="Major★7.5M★";

                else

                userTitle="Director★7.5M★";

          }

          else{

                if(bb)

                userTitle="Major★7.5M★";

                else

                userTitle="Director★7.5M★";

          }

          break;

    case 12:

          if(legend.equals("男")){

                if(bb)

                userTitle="Lieutenant Colonel★8M★";

                else

                userTitle="Senior Director★8M★";

          }

          else{

                if(bb)

                userTitle="Lieutenant Colonel★8M★";

                else

                userTitle="Senior Director★8M★";

          }

          break;

    case 13:

          if(legend.equals("男")){

                if(bb)

                userTitle="Colonel★25M★";

                else

                userTitle="VP★25M★";

          }

          else{

                if(bb)

                userTitle="Colonel★25M★";

                else

                userTitle="VP★25M★";

          }

          break;

    case 14:

          if(legend.equals("男")){

                if(bb)

                userTitle="Brigadier General★50M★";

                else

                userTitle="Senior VP★50M★";

          }

          else{

                if(bb)

                userTitle="Brigadier General★50M★";

                else

                userTitle="Senior VP★50M★";

          }

          break;

    case 15:

          if(legend.equals("男")){

                if(bb)

                userTitle="Major General★75M★";

                else

                userTitle="Executive★75M★";

          }

          else{

                if(bb)

                userTitle="Major General★75M★";

                else

                userTitle="Executive★75M★";

          }

          break;

    case 16:

          if(legend.equals("男")){

                if(bb)

                userTitle="Lieutenant General★100M★";

                else

                userTitle="CTO★100M★";

          }

          else{

                if(bb)

                userTitle="Lieutenant General★100M★";

                else

                userTitle="CTO★100M★";

          }

          break;

    case 17:

          if(legend.equals("男")){

                if(bb)

                userTitle="General★300M★";

                else

                userTitle="COO★300M★";

          }

          else{

                if(bb)

                userTitle="General★300M★";

                else

                userTitle="COO★300M★";

          }

          break;

    case 18:

          if(legend.equals("男")){

                if(bb)

                userTitle="Theater General★600M★";

                else

                userTitle="CFO★600M★";

          }

          else{

                if(bb)

                userTitle="Theater General★600M★";

                else

                userTitle="CFO★600M★";

          }

          break;

    case 19:

          if(legend.equals("男")){

                if(bb)

                userTitle="Chief General★1G★";

                else

                userTitle="CXO★1G★";

          }

          else{

                if(bb)

                userTitle="Chief General★1G★";

                else

                userTitle="CXO★1G★";

          }

          break;

    case 20:

          if(legend.equals("男")){

                if(bb)

                userTitle="Secretary★1G★";

                else

                userTitle="CEO★1G★";

          }

          else{

                if(bb)

                userTitle="Secretary★1G★";

                else

                userTitle="CEO★1G★";

          }

          break;

    case 21:

          if(legend.equals("男")){

                if(bb)

                userTitle="Present★2G★";

                else

                userTitle="Chairman★2G★";

          }

          else{

                if(bb)

                userTitle="Present★2G★";

                else

                userTitle="Chairman★2G★";

          }

          break;

}

return userTitle;

}




*/


/**
 * 返回头称
 */
private String getUserTitle(int level,String legend){
	int a = (int) (Math.random()*2);
	boolean bb = false;
	if(a>1)
	{
		bb = true;
	}else{
		bb = false;
	}
	String userTitle = "无";
	switch (level) {
	case 1:
		if(legend.equals("男")){
			if(bb)
			userTitle="庶民★1K★";
			else
			userTitle="地球修建者★1K★";
		}
		else{
			if(bb)
			userTitle="布衣天使★1K★";
			else
			userTitle="小婢★1K★";
		}
		break;
	case 2:
		if(legend.equals("男")){
			if(bb)
			userTitle="秀才★10K★";
			else
			userTitle="工匠★10K★";
		}
		else{
			if(bb)
			userTitle="绣花姑娘★10K★";
			else
			userTitle="青衣丫环★10K★";
		}
		break;
	case 3:
		if(legend.equals("男")){
			if(bb)
			userTitle="举人★50K★";
			else
			userTitle="赤脚医生★50K★";
		}
		else{
			if(bb)
			userTitle="小家碧玉★50K★";
			else
			userTitle="侍女★50K★";
		}
		break;
	case 4:
		if(legend.equals("男")){
			if(bb)
			userTitle="道士★100K★";
			else
			userTitle="教书先生★100K★";
		}
		else{
			if(bb)
			userTitle="大家闺秀★100K★";
			else
			userTitle="舞女★100K★";
		}
		break;
	case 5:
		if(legend.equals("男")){
			if(bb)
			userTitle="进士★250K★";
			else
			userTitle="船家★250K★";
		}
		else{
			if(bb)
			userTitle="宫女★250K★";
			else
			userTitle="才女★250K★";
		}
		break;
	case 6:
		if(legend.equals("男")){
			if(bb)
			userTitle="巡检★500K★";
			else
			userTitle="富商★500K★";
		}
		else{
			if(bb)
			userTitle="美貌少女★500K★";
			else
			userTitle="侍女★500K★";
		}
		break;
	case 7:
		if(legend.equals("男")){
			if(bb)
			userTitle="庄园园主★750K★";
			else
			userTitle="魔术师★750K★";
		}
		else{
			if(bb)
			userTitle="妙龄少女★750K★";
			else
			userTitle="绝代佳人★750K★";
		}
		break;
	case 8:
		if(legend.equals("男")){
			if(bb)
			userTitle="山寨王★1M★";
			else
			userTitle="酋长★1M★";
		}
		else{
			if(bb)
			userTitle="婵娟★1M★";
			else
			userTitle="玉女★1M★";
		}
		break;
	case 9:
		if(legend.equals("男")){
			if(bb)
			userTitle="典史★2.5M★";
			else
			userTitle="骁骑卫★2.5M★";
		}
		else{
			if(bb)
			userTitle="西施★2.5M★";
			else
			userTitle="窈窕淑女★2.5M★";
		}
		break;
	case 10:
		if(legend.equals("男")){
			if(bb)
			userTitle="统帅★5M★";
			else
			userTitle="教皇★5M★";
		}
		else{
			if(bb)
			userTitle="红粉佳人★5M★";
			else
			userTitle="花仙子★5M★";
		}
		break;
	case 11:
		if(legend.equals("男")){
			if(bb)
			userTitle="通政使★7.5M★";
			else
			userTitle="参将★7.5M★";
		}
		else{
			if(bb)
			userTitle="宫廷礼仪★7.5M★";
			else
			userTitle="巾帼女郎★7.5M★";
		}
		break;
	case 12:
		if(legend.equals("男")){
			if(bb)
			userTitle="大学士★8M★";
			else
			userTitle="护驾大将军★8M★";
		}
		else{
			if(bb)
			userTitle="女议员★8M★";
			else
			userTitle="侠女★8M★";
		}
		break;
	case 13:
		if(legend.equals("男")){
			if(bb)
			userTitle="提督★25M★";
			else
			userTitle="都统★25M★";
		}
		else{
			if(bb)
			userTitle="富豪千金★25M★";
			else
			userTitle="华裔女郎★25M★";
		}
		break;
	case 14:
		if(legend.equals("男")){
			if(bb)
			userTitle="巡抚★50M★";
			else
			userTitle="使节★50M★";
		}
		else{
			if(bb)
			userTitle="嫔妃★50M★";
			else
			userTitle="华衣夫人★50M★";
		}
		break;
	case 15:
		if(legend.equals("男")){
			if(bb)
			userTitle="总督★75M★";
			else
			userTitle="护驾大将军★75M★";
		}
		else{
			if(bb)
			userTitle="女强人★75M★";
			else
			userTitle="诰命夫人★75M★";
		}
		break;
	case 16:
		if(legend.equals("男")){
			if(bb)
			userTitle="太保★100M★";
			else
			userTitle="大学士★100M★";
		}
		else{
			if(bb)
			userTitle="佳丽★100M★";
			else
			userTitle="公爵夫人★100M★";
		}
		break;
	case 17:
		if(legend.equals("男")){
			if(bb)
			userTitle="镇国公★300M★";
			else
			userTitle="辅国公★300M★";
		}
		else{
			if(bb)
			userTitle="后妃★300M★";
			else
			userTitle="格格★300M★";
		}
		break;
	case 18:
		if(legend.equals("男")){
			if(bb)
			userTitle="贝勒★600M★";
			else
			userTitle="阿哥★600M★";
		}
		else{
			if(bb)
			userTitle="郡主★600M★";
			else
			userTitle="公主★600M★";
		}
		break;
	case 19:
		if(legend.equals("男")){
			if(bb)
			userTitle="亲王★1G★";
			else
			userTitle="郡王★1G★";
		}
		else{
			if(bb)
			userTitle="福晋★1G★";
			else
			userTitle="王妃★1G★";
		}
		break;
	case 20:
		if(legend.equals("男")){
			if(bb)
			userTitle="硕亲王★1G★";
			else
			userTitle="多罗郡王★1G★";
		}
		else{
			if(bb)
			userTitle="贵妃★1G★";
			else
			userTitle="皇后★1G★";
		}
		break;
	case 21:
		if(legend.equals("男")){
			if(bb)
			userTitle="皇帝★2G★";
			else
			userTitle="岛国国王★2G★";
		}
		else{
			if(bb)
			userTitle="女王★2G★";
			else
			userTitle="皇后★2G★";
		}
		break;
}
return userTitle;
}

/*
private String getUserTitle(int level,String legend){
	int a = (int) (Math.random()*2);
	boolean bb = false;
	if(a>1)
	{
		bb = true;
	}else{
		bb = false;
	}
	String userTitle = "無";
	switch (level) {
	case 1:
		if(legend.equals("男")){
			if(bb)
			userTitle="庶民★1K★";
			else
			userTitle="地球修建者★1K★";
		}
		else{
			if(bb)
			userTitle="布衣天使★1K★";
			else
			userTitle="小婢★1K★";
		}
		break;
	case 2:
		if(legend.equals("男")){
			if(bb)
			userTitle="秀才★10K★";
			else
			userTitle="工匠★10K★";
		}
		else{
			if(bb)
			userTitle="繡花姑娘★10K★";
			else
			userTitle="青衣丫環★10K★";
		}
		break;
	case 3:
		if(legend.equals("男")){
			if(bb)
			userTitle="舉人★50K★";
			else
			userTitle="赤腳醫生★50K★";
		}
		else{
			if(bb)
			userTitle="小家碧玉★50K★";
			else
			userTitle="侍女★50K★";
		}
		break;
	case 4:
		if(legend.equals("男")){
			if(bb)
			userTitle="道士★100K★";
			else
			userTitle="教書先生★100K★";
		}
		else{
			if(bb)
			userTitle="大家閨秀★100K★";
			else
			userTitle="舞女★100K★";
		}
		break;
	case 5:
		if(legend.equals("男")){
			if(bb)
			userTitle="進士★250K★";
			else
			userTitle="船家★250K★";
		}
		else{
			if(bb)
			userTitle="宮女★250K★";
			else
			userTitle="才女★250K★";
		}
		break;
	case 6:
		if(legend.equals("男")){
			if(bb)
			userTitle="巡檢★500K★";
			else
			userTitle="富商★500K★";
		}
		else{
			if(bb)
			userTitle="美貌少女★500K★";
			else
			userTitle="侍女★500K★";
		}
		break;
	case 7:
		if(legend.equals("男")){
			if(bb)
			userTitle="莊園園主★750K★";
			else
			userTitle="魔術師★750K★";
		}
		else{
			if(bb)
			userTitle="妙齡少女★750K★";
			else
			userTitle="絕代佳人★750K★";
		}
		break;
	case 8:
		if(legend.equals("男")){
			if(bb)
			userTitle="山寨王★1M★";
			else
			userTitle="酋長★1M★";
		}
		else{
			if(bb)
			userTitle="嬋娟★1M★";
			else
			userTitle="玉女★1M★";
		}
		break;
	case 9:
		if(legend.equals("男")){
			if(bb)
			userTitle="典史★2.5M★";
			else
			userTitle="驍騎衛★2.5M★";
		}
		else{
			if(bb)
			userTitle="西施★2.5M★";
			else
			userTitle="窈窕淑女★2.5M★";
		}
		break;
	case 10:
		if(legend.equals("男")){
			if(bb)
			userTitle="統帥★5M★";
			else
			userTitle="教皇★5M★";
		}
		else{
			if(bb)
			userTitle="紅粉佳人★5M★";
			else
			userTitle="花仙子★5M★";
		}
		break;
	case 11:
		if(legend.equals("男")){
			if(bb)
			userTitle="通政使★7.5M★";
			else
			userTitle="參將★7.5M★";
		}
		else{
			if(bb)
			userTitle="宮廷禮儀★7.5M★";
			else
			userTitle="巾幗女郎★7.5M★";
		}
		break;
	case 12:
		if(legend.equals("男")){
			if(bb)
			userTitle="大學士★8M★";
			else
			userTitle="護駕大將軍★8M★";
		}
		else{
			if(bb)
			userTitle="女議員★8M★";
			else
			userTitle="俠女★8M★";
		}
		break;
	case 13:
		if(legend.equals("男")){
			if(bb)
			userTitle="提督★25M★";
			else
			userTitle="都統★25M★";
		}
		else{
			if(bb)
			userTitle="富豪千金★25M★";
			else
			userTitle="華裔女郎★25M★";
		}
		break;
	case 14:
		if(legend.equals("男")){
			if(bb)
			userTitle="巡撫★50M★";
			else
			userTitle="使節★50M★";
		}
		else{
			if(bb)
			userTitle="嬪妃★50M★";
			else
			userTitle="華衣夫人★50M★";
		}
		break;
	case 15:
		if(legend.equals("男")){
			if(bb)
			userTitle="總督★75M★";
			else
			userTitle="護駕大將軍★75M★";
		}
		else{
			if(bb)
			userTitle="女強人★75M★";
			else
			userTitle="誥命夫人★75M★";
		}
		break;
	case 16:
		if(legend.equals("男")){
			if(bb)
			userTitle="太保★100M★";
			else
			userTitle="大學士★100M★";
		}
		else{
			if(bb)
			userTitle="佳麗★100M★";
			else
			userTitle="公爵夫人★100M★";
		}
		break;
	case 17:
		if(legend.equals("男")){
			if(bb)
			userTitle="鎮國公★300M★";
			else
			userTitle="輔國公★300M★";
		}
		else{
			if(bb)
			userTitle="後妃★300M★";
			else
			userTitle="格格★300M★";
		}
		break;
	case 18:
		if(legend.equals("男")){
			if(bb)
			userTitle="貝勒★600M★";
			else
			userTitle="阿哥★600M★";
		}
		else{
			if(bb)
			userTitle="郡主★600M★";
			else
			userTitle="公主★600M★";
		}
		break;
	case 19:
		if(legend.equals("男")){
			if(bb)
			userTitle="親王★1G★";
			else
			userTitle="郡王★1G★";
		}
		else{
			if(bb)
			userTitle="福晉★1G★";
			else
			userTitle="王妃★1G★";
		}
		break;
	case 20:
		if(legend.equals("男")){
			if(bb)
			userTitle="碩親王★1G★";
			else
			userTitle="多羅郡王★1G★";
		}
		else{
			if(bb)
			userTitle="貴妃★1G★";
			else
			userTitle="皇后★1G★";
		}
		break;
	case 21:
		if(legend.equals("男")){
			if(bb)
			userTitle="皇帝★2G★";
			else
			userTitle="島國國王★2G★";
		}
		else{
			if(bb)
			userTitle="女王★2G★";
			else
			userTitle="皇后★2G★";
		}
		break;
}
return userTitle;
}
*/


/**
 * 加经验
 */
   public void addCharm_value(int count, int userid)
   {
     PreparedStatement ps = null;
     int i = 0;
     String sql = null;
     Connection con = null;
     try {
       con = conpool.getConnection();
       if (con == null) 
       {
         this.logger.error("addCharm_value  return null conneciont to db");
         return;
       }
 
       sql = SQLInterface.ADD_CHARMVALUE;//"update poke_user set exp_value = exp_value+? where user_id = ?";
       ps = con.prepareStatement(sql);
       ps.setInt(1, count);
       ps.setInt(2, userid);
 
       i = ps.executeUpdate();
       if (this.on_off) 
       {
         System.out.println("。。。。。增加经验值。。。。。" + i);
       }
     }
     catch (SQLException e)
     {
       e.printStackTrace();
       closeDBRS(ps);
       conpool.returnConnectionPool(con);
       this.logger.error("method:userOut throw error:{}", e.getMessage());
       if (e.getMessage().contains("OutOfMemoryError"))
       {
         System.gc();
       }
     }
     closeDBRS(ps);
     conpool.returnConnectionPool(con);
     con = null;
   }
 
   /**
    * 检查用户是不是在关联列表里面
    */
   public int cheUser(int userid) 
   {
     PreparedStatement ps = null;
     ResultSet rs = null;
     int i = 0;
     String sql = null;
     Connection con = null;
     try 
     {
       con = conpool.getConnection();
       if (con == null) 
       {
         conpool.returnConnectionPool(con);
         this.logger.error("cheUser  return null conneciont to db");
         return 0;
       }
       sql = SQLInterface.CHECK_USER;//"select count(*) from table_user_ref where user_id =?";
       ps = con.prepareStatement(sql);
       ps.setInt(1, userid);
 
       rs = ps.executeQuery();
       while (rs.next()) 
       {
         i = rs.getInt(1);
       }
 
     }
     catch (SQLException e)
     {
       e.printStackTrace();
       closeDBRS(rs, ps);
       conpool.returnConnectionPool(con);
       this.logger.error("method: cheUser throw error:{}", e.getMessage());
       if (e.getMessage().contains("OutOfMemoryError"))
       {
         System.gc();
       }
     }
     closeDBRS(rs, ps);
     conpool.returnConnectionPool(con);
     con = null;
     return i;
   }
 
   /**
    * 扣税
    */
   public int deductVIP(int user_id)
   {
     PreparedStatement ps = null;
 
     int i = 0;
     String sql = null;
     Connection con = null;
     try 
     {
       con = conpool.getConnection();
       if (con == null) {
         conpool.returnConnectionPool(con);
         this.logger.error("cheUser  return null conneciont to db");
         return 0;
       }
       sql = SQLInterface.DEDCUT_VIP;//"update poke_user set USER_ALL_MONEY = USER_ALL_MONEY-? where user_id = ?";
       ps = con.prepareStatement(sql);
       ps.setInt(1, PokeServer.money);
       ps.setInt(2, user_id);
 
       i = ps.executeUpdate();
     }
     catch (SQLException e)
     {
       closeDBRS(ps);
 
       this.logger.error("method: cheUser throw error:{}", e.getMessage());
       if (e.getMessage().contains("OutOfMemoryError"))
       {
         System.gc();
       }
     }
     closeDBRS(ps);
     conpool.returnConnectionPool(con);
     con = null;
     System.out.println("扣钱是否成功：" + i);
     return i;
   }
 }

